package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Sort;
import com.woniu.service.ISortService;
import com.woniu.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanMax
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/sort")
@Api(tags = "分类管理")
public class SortController extends BaseController{
    @Autowired
    private ISortService sortService;

    @GetMapping
    @ApiOperation("获取所有分类")
    public Result getList(){
        List<Sort> list = sortService.list();
        return toDataResult(list);
    }
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索获取商品分类")
    public Result search(@RequestBody Sort sort, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        PageResult list = sortService.search(sort,currentPage,pageSize);
        return toDataResult(list);
    }
    @PostMapping("/add")
    @ApiOperation("添加商品分类")
    public Result add(@RequestBody Sort sort){
        boolean save = sortService.save(sort);
        return toResult(save);
    }
    @PutMapping("/edit")
    @ApiOperation("修改商品分类")
    public Result edit(@RequestBody Sort sort){
        boolean save = sortService.updateById(sort);
        return toResult(save);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("删除商品分类")
    public Result deleteById(@PathVariable String id){
        Integer b = sortService.deleteByIdAll(id);
        return toResult(b);
    }
    @GetMapping("/get/{id}")
    @ApiOperation("通过分类id获取分类")
    public Result getById(@PathVariable String id){
        Sort sort = sortService.getById(id);
        return toDataResult(sort);
    }
}
